package com.dingshi.security;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.PatrolApi;
import com.dingshi.security.db.SettingLoader;

public class AlarmReceiver extends BroadcastReceiver {

	private static final String TAG = "AlamrReceiver";
	private Context mContext;
	@Override
	public void onReceive(Context context, Intent intent) {
		this.mContext = context;
		getNofication();
	}

	private void getNofication() {
		Log.e("Register", "register is running");
		PatrolApi api = new PatrolApi(mContext);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				JSONObject jsonEntity = apiResult.getJsonEntity();
				if (jsonEntity != null) {
					// {"d":[{"__type":"DiaryReturn:r","c":"aaa","t":"01:59:05"}],"t":1}

					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0:// 对象
							break;
						case 1:// 数组
							

							break;
						case -1:// 错误
							
							break;

						default:
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
			}
		});
		Location location = SecurityApp.getApp(mContext).getLocation();
		if(location!=null) {
			api.reportPatrol(1, mContext, SettingLoader.getDepartNum(mContext), SettingLoader.getAreaNum(mContext), SettingLoader.getGuardNum(mContext), location.getLongitude()+"", location.getLatitude()+"");
		}
	}
	
	/*private Location getLocation(Context context) {
		LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
			return locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		}else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			return locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		}
		return null;
	}*/
}
